import CustomRenderDebugger from "./CustomRenderDebugger/CustomRenderDebugger";
import TextureGrid from "./Scene/TextureGrid/TextureGrid";

const { ccclass, property } = cc._decorator;

@ccclass
export default class HelloTest extends cc.Component {

    @property(cc.Node)
    public gridNode: cc.Node = null;

    start() {
    }

    onSetGrid() {
        let textureGridComponent = this.gridNode.getComponent(TextureGrid);
        let gridElement = textureGridComponent.getGridAt(1, 1);
        gridElement.position.x += 50;
        gridElement.position.y += 50;
        textureGridComponent.setGridAt(1, 1, gridElement);

    }

    onGridTween() {
        let textureGridComponent = this.gridNode.getComponent(TextureGrid);
        let gridElement = textureGridComponent.getGridAt(1, 1);
        let timer = 0;
        this.schedule(function(dt) {
            if (timer >= 3) {
                return
            }
            gridElement.position.x += dt * 10;
            gridElement.position.y += dt * 10;
            textureGridComponent.setGridAt(1, 1, gridElement);
            timer += dt;
        });
    }

    onTestEffect() {
        let textureGridComponent = this.gridNode.getComponent(TextureGrid);
        let timer = 0;
        this.schedule(function(dt) {
            if (timer >= 10) {
                return
            }
            for (let i = 0; i < 30; i++) {
                let gridElement = textureGridComponent.randomGrid();
                gridElement.position.x += dt * (Math.random() * 20 - 10);
                gridElement.position.y += dt * (Math.random() * 20 - 10);
                textureGridComponent.updateGrid(gridElement);
            }
            timer += dt;
        });
    }

    onGridRestore() {
        let textureGridComponent = this.gridNode.getComponent(TextureGrid);
        textureGridComponent.restore();
    }

    onMaskToggle(toggle) {
        let textureGridComponent = this.gridNode.getComponent(TextureGrid);
        textureGridComponent.isMask = toggle.isChecked;
    }

    onCascadeToggle(toggle) {
        let textureGridComponent = this.gridNode.getComponent(TextureGrid);
        textureGridComponent.isCascade = toggle.isChecked;
    }

    onTileToggle(toggle) {
        let textureGridComponent = this.gridNode.getComponent(TextureGrid);
        textureGridComponent.isTile = toggle.isChecked;
    }

    onDebugToggle(toggle) {
        this.gridNode.getComponent(CustomRenderDebugger).enabled = toggle.isChecked;
    }
}
